{% extends "base.html" %}

{% block title %}报告生成{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">生成分析报告</h4>
                </div>
                <div class="card-body">
                    {% with messages = get_flashed_messages(with_categories=true) %}
                        {% if messages %}
                            {% for category, message in messages %}
                            <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                                {{ message }}
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                            </div>
                            {% endfor %}
                        {% endif %}
                    {% endwith %}

                    {% if date_range.start and date_range.end %}
                    <div class="alert alert-info mb-3">
                        <i class="fas fa-info-circle"></i> 
                        可选择的数据范围：{{ date_range.start }} 至 {{ date_range.end }}
                    </div>
                    {% endif %}

                    <form action="{{ url_for('reports.generate') }}" method="post">
                        <div class="mb-3">
                            <label for="report_type" class="form-label">报告类型</label>
                            <select class="form-select" id="report_type" name="report_type" required>
                                <option value="">请选择报告类型</option>
                                <option value="hardness">硬度分析报告</option>
                                <option value="marker">分子标记分析报告</option>
                            </select>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="start_date" class="form-label">开始日期</label>
                                    <input type="date" class="form-control" id="start_date" name="start_date" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="end_date" class="form-label">结束日期</label>
                                    <input type="date" class="form-control" id="end_date" name="end_date" required>
                                </div>
                            </div>
                        </div>

                        <div class="text-center">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-file-export"></i> 生成报告
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
    // 设置日期选择器的默认值和范围
    const today = new Date().toISOString().split('T')[0];
    const oneMonthAgo = new Date();
    oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
    const oneMonthAgoStr = oneMonthAgo.toISOString().split('T')[0];
    
    // 设置默认值：开始日期为一个月前，结束日期为今天
    $('#start_date').val(oneMonthAgoStr);
    $('#end_date').val(today);
    
    // 设置最大值为今天
    $('#end_date').attr('max', today);
    $('#start_date').attr('max', today);
    
    // 开始日期变化时更新结束日期的最小值
    $('#start_date').change(function() {
        $('#end_date').attr('min', $(this).val());
        if ($('#end_date').val() < $(this).val()) {
            $('#end_date').val($(this).val());
        }
    });
    
    // 结束日期变化时更新开始日期的最大值
    $('#end_date').change(function() {
        $('#start_date').attr('max', $(this).val());
        if ($('#start_date').val() > $(this).val()) {
            $('#start_date').val($(this).val());
        }
    });
    
    // 表单验证
    $('form').on('submit', function(e) {
        const startDate = new Date($('#start_date').val());
        const endDate = new Date($('#end_date').val());
        const today = new Date();
        
        if (startDate > endDate) {
            e.preventDefault();
            alert('开始日期不能晚于结束日期');
            return false;
        }
        
        if (startDate > today || endDate > today) {
            e.preventDefault();
            alert('不能选择未来的日期');
            return false;
        }
    });
});
</script>
{% endblock %} 